package com.ktjy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pojo.Lost;
import com.ktjy.pojo.SysUser;
import com.ktjy.service.ILostService;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-01-16
 */
@Controller
@RequestMapping("/lost")
public class LostController {
    Logger logger = LoggerFactory.getLogger(LostController.class);

    @Autowired
    ILostService iLostService;

    @RequestMapping("/list")
    public String list(Lost lost,
                       @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                       HttpSession session,
                       Model model) {
        try {
            //组装分页条件
            Page<Lost> page = new Page<>(pageIndex, pageSize);
            //组装过滤条件 动态sql
            QueryWrapper<Lost> qw = new QueryWrapper();
            if (lost.getLstCustName() != null && !lost.getLstCustName().equals("")) {
                qw.like("lst_cust_name", lost.getLstCustName());
            }
            if (lost.getLstCustManagerName() != null && !lost.getLstCustManagerName().equals("")) {
                qw.like("lst_cust_manager_name", lost.getLstCustManagerName());
            }
            if (lost.getLstStatus() != null && !lost.getLstStatus().equals("")) {
                qw.eq("lst_status", lost.getLstStatus());
            }
            //客户经理只查自己创建的客户数据
            SysUser sysUser = (SysUser) session.getAttribute("loginUser");
            if (sysUser.getUsrRoleId() == 2) {//客户经理
                qw.eq("lst_cust_manager_id", sysUser.getUsrId());
            }
            //调用分页方法
            IPage<Lost> pager = iLostService.page(page, qw);
            model.addAttribute("pager", pager);

            //回显数据
            model.addAttribute("condition", lost);
        } catch (Exception e) {
            logger.error("LostController 中的 list 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "lost/list";
    }

    /**
     * 跳转暂缓流失页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/addDefer")
    public String addDefer(@RequestParam("id") Long id, Model model) {
        try {
            Lost lost = iLostService.getById(id);
            model.addAttribute("lost", lost);
        } catch (Exception e) {
            logger.error("LostController 中的 addDefer 方法执行发生错误。", e);
            throw new RuntimeException(e);
        }
        return "lost/defer";
    }

    /**
     * 保存暂缓流失数据
     *
     * @param lost
     * @return
     */
    @RequestMapping("/saveDefer")
    public String saveDefer(Lost lost) {
        try {
            lost.setLstStatus("2");//修改为暂缓流失
            boolean ok = iLostService.updateById(lost);
            if (ok) {
                //跳转到列表;重定向到分页方法
                return "redirect:/lost/list";
            } else {
                //跳回新增页面
                return "redirect:/lost/addDefer";
            }
        } catch (Exception e) {
            logger.error("LostController 中的 saveDefer 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 跳转确认流失页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/addConfirm")
    public String addConfirm(@RequestParam("id") Long id, Model model) {
        try {
            Lost lost = iLostService.getById(id);
            model.addAttribute("lost", lost);
    } catch (Exception e) {
        logger.error("LostController 中的 addConfirm 方法执行发生错误。 ", e);
        throw new RuntimeException(e);
    }
        return "lost/confirm";
    }

    /**
     * 保存确认流失数据
     * @param lost
     * @param model
     * @return
     */
    @RequestMapping("/saveConfirm")
    public String saveConfirm(Lost lost,Model model){
        try {
            lost.setLstStatus("3");//修改为确认流失
            lost.setLstLostDate(LocalDateTime.now());//确认流失时间
            boolean ok = iLostService.update(lost);
            if (ok) {
                //跳转到列表;重定向到分页方法
                return "redirect:/lost/list";
            } else {
                //跳回新增页面
                return "redirect:/lost/addConfirm";
            }
        } catch (Exception e) {
            logger.error("LostController 中的 saveConfirm 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
    }
}
